package com.vhuomai.product.home.controller;

import com.quanyue.auto.util.ServiceResult;
import com.quanyue.auto.web.Scope;
import com.vhuomai.product.home.dto.HomeEntranceResult;
import com.vhuomai.product.home.entity.BaseHomeEntrance;
import com.vhuomai.product.home.service.HomeEntranceService;
import com.vhuomai.system.constant.UserRole;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Cui
 */
@Api(tags = "首页入口")
@RestController
@RequestMapping("/api")
public class HomeEntranceController {
    @Autowired
    private HomeEntranceService homeEntranceService;

    @ApiOperation(value = "添加入口")
    @Scope(role = UserRole.MANAGER)
    @PostMapping(value = "/home/entrance", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<Boolean> createHomeEntrance(@RequestBody BaseHomeEntrance baseHomeEntrance) {
        homeEntranceService.createHomeEntrance(baseHomeEntrance);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "更新入口")
    @Scope(role = UserRole.MANAGER)
    @PutMapping(value = "/home/entrance", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<Boolean> updateHomeEntrance(@RequestBody BaseHomeEntrance baseHomeEntrance) {
        homeEntranceService.updateHomeEntrance(baseHomeEntrance);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "删除入口")
    @Scope(role = UserRole.MANAGER)
    @DeleteMapping(value = "/home/entrance", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<Boolean> deleteHomeEntrance(@RequestParam("id") Integer id) {
        homeEntranceService.deleteHomeEntrance(id);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "入口列表")
    @GetMapping(value = "/home/entrance", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<List<HomeEntranceResult>> homeEntranceList() {
        return ServiceResult.success(homeEntranceService.homeEntranceList());
    }
}
